
//  Created by 邓盛明 on 2022/2/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFPPTFunctionView: UIView {
    @objc public var topView:UIView!
    @objc public var bottomView:UIView!

    /**网络速度提示*/
    @objc public var networkSpeedView:UIView!
    //只显示一次
    @objc public var isShowOnce:Bool = false

    /**返回*/
    @objc public var backBtn = TalkfunIncreaseButton.init()

    /**刷新*/
    @objc public var refreshBtn = TalkfunIncreaseButton.init()

    /**全屏*/
    @objc public var fullScreenBtn = TalkfunIncreaseButton.init()

    /**课程名称*/
    @objc public var titleLab:UILabel!
    @objc public var hotImg:UIImageView!
    @objc public var numberHot:UILabel!

    /**切换线路*/
    @objc public var switchlineBtn:TalkfunIncreaseButton!


    /**横屏刷新*/
    @objc public var heng_refreshBtn:TalkfunIncreaseButton!

    /**更多*/
    @objc public var more_Btn:TalkfunIncreaseButton!

    //横竖屏   YES: 横屏  NO:竖屏
    @objc public var isOrientationLandscape:Bool = false
    @objc public var placeholder:String!

    var bottom_gl:CAGradientLayer!
    var top_gl:CAGradientLayer!
    var labView:UIView!
    var lineLab:UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.networkSpeedView = UIView.init()
        self.networkSpeedView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.6)
        self.networkSpeedView.isHidden = true
        addSubview(self.networkSpeedView)
        
        self.bottomView = UIView.init()
        addSubview(self.bottomView)
        
        self.bottom_gl = CAGradientLayer.init()
       
        self.bottom_gl.startPoint = CGPoint(x: 0.5, y: 0.08888889104127884)

        self.bottom_gl.endPoint = CGPoint(x: 0.5, y: 1)
        self.bottom_gl.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.0).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.35).cgColor]
        self.bottom_gl.locations = [0,1];
        self.bottomView.layer.addSublayer(self.bottom_gl)
       
        
        self.topView = UIView.init()
        addSubview(self.topView)
        
        self.top_gl = CAGradientLayer.init()
        self.top_gl.startPoint = CGPoint(x: 0.5, y: 0.9394475817680359)
        self.top_gl.endPoint = CGPoint(x: 0.5, y: 0)
        self.top_gl.colors = [UIColor(red: 0, green: 0, blue: 0, alpha: 0.0).cgColor, UIColor(red: 0, green: 0, blue: 0, alpha: 0.05).cgColor,UIColor(red: 0, green: 0, blue: 0, alpha: 0.35).cgColor]
        self.top_gl.locations =  [0,0.2,1];
        self.topView.layer.addSublayer(self.top_gl)
   

        self.backBtn.setImage(UIImage.init(named: "HTLive.bundle/TF_return"), for: .normal)

        self.backBtn.tag = 200;
        self.topView.addSubview(self.backBtn)
        
      
        

        self.refreshBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_reflesh"), for: .normal)
        self.refreshBtn.tag = 203;

        self.bottomView.addSubview(self.refreshBtn)
        

        self.fullScreenBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_max"), for: .normal)
        self.fullScreenBtn.tag = 206;
        self.bottomView.addSubview(self.fullScreenBtn)
        
        self.titleLab = UILabel.init()
        
        self.titleLab.font = UIFont.PingFang(size: 15)
        self.titleLab.text = "11111111";
        self.titleLab.textColor = UIColor.white
        self.topView.addSubview(self.titleLab)
        
        self.more_Btn = TalkfunIncreaseButton.init()
        self.more_Btn.setImage(UIImage.init(named: "HTLive.bundle/largeLive_more"), for: .normal)
      
        self.more_Btn.isHidden = true
        self.more_Btn.tag = 2001
        self.topView.addSubview(self.more_Btn)
        
        self.heng_refreshBtn = TalkfunIncreaseButton.init()
        self.heng_refreshBtn.setImage(UIImage.init(named: "HTLive.bundle/mask_icon_reflesh"), for: .normal)
   
        self.heng_refreshBtn.tag = 203;
        self.heng_refreshBtn.isHidden = true;
        self.topView.addSubview(self.heng_refreshBtn)
     
        
        self.labView = UIView.init()
        self.labView.backgroundColor = UIColor.clear
        self.networkSpeedView.addSubview(self.labView)

        
        self.lineLab = UILabel.init()
        self.lineLab.text = "当前网络卡顿,试试";
        self.lineLab.textColor = UIColor.white
        self.lineLab.font = UIFont.PingFang(size: 16)
        self.labView.addSubview(self.lineLab)
        
        self.switchlineBtn = TalkfunIncreaseButton.init()
        self.switchlineBtn.setTitle("切换线路", for: .normal)
        self.switchlineBtn.setTitleColor(UIColor(red: 5/255.0, green: 211/255.0, blue: 128/255.0, alpha: 1.0), for: .normal)
        self.switchlineBtn.titleLabel?.font = UIFont.PingFang(size: 16)
        self.switchlineBtn.tag = 211;
        self.labView.addSubview(self.switchlineBtn)
      
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.topView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)

        
        if (self.isOrientationLandscape) {
            self.bottomView.frame = CGRect(x: 0, y: self.frame.size.height-64, width: self.frame.size.width, height: 64)
            self.refreshBtn.isHidden = true
            self.fullScreenBtn.isHidden = true
          
        } else {
            self.bottomView.frame = CGRect(x: 0, y: self.frame.size.height-44, width: self.frame.size.width, height: 44)
            self.refreshBtn.isHidden = false
            self.fullScreenBtn.isHidden = false
         
        }
      
        self.bottom_gl.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)
        self.top_gl.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 44)
        self.backBtn.frame = CGRect(x: 15, y: 10, width: 24, height: 24)

        self.titleLab.frame = CGRect(x: self.backBtn.frame.maxX+10, y: 2, width: self.frame.size.width-70, height: 40)
     

      
        self.refreshBtn.frame = CGRect(x: 15, y: 10, width: 24, height: 24)
        self.fullScreenBtn.frame = CGRect(x: self.bottomView.frame.size.width-45, y: 10, width: 24, height: 24)
        
        self.networkSpeedView.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: 40)
       
        self.labView.frame = CGRect(x: (self.networkSpeedView.frame.size.width-214)/2.0, y: 9, width: 214, height: 22)
        self.lineLab.frame = CGRect(x: 0, y: 0, width: 135, height: 22)
        self.switchlineBtn.frame = CGRect(x: 135, y: 0, width: 79, height: 22)

        //横屏设置
        self.more_Btn.frame = CGRect(x: self.frame.size.width-40-24, y: 10, width: 24, height: 24)
        
        self.heng_refreshBtn.frame = CGRect(x: self.more_Btn.frame.origin.x-32-24, y: 10, width: 24, height: 24)
    }
    
    @objc public func buttonsAddTarget(_ target:NSObject, action:Selector) {
        self.backBtn.addTarget(target, action: action, for: .touchUpInside)
        self.refreshBtn.addTarget(target, action: action, for: .touchUpInside)
        self.fullScreenBtn.addTarget(target, action: action, for: .touchUpInside)
        self.switchlineBtn.addTarget(target, action: action, for: .touchUpInside)
        self.heng_refreshBtn.addTarget(target, action: action, for: .touchUpInside)
        self.more_Btn.addTarget(target, action: action, for: .touchUpInside)
    }
    
    //开始显示网络卡顿
   @objc public func stratSpeedUI()  {
       
       self.bringSubviewToFront(self.networkSpeedView)
        self.isShowOnce = true;
        self.networkSpeedView.isHidden = false
       
       
       self.timerInvalidate()
       
       DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+3) {
           self.hideSpeedView()
              }
       
//        self.timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(hideSpeedView), userInfo: nil, repeats: false)
//        RunLoop.main.add(self.timer, forMode: .commonModes)
      
    }
    @objc func timerInvalidate()
    
    {
//        if((self.timer) != nil)
//        {
//            self.timer.invalidate()
//            self.timer = nil
//        }
    }
   @objc func hideSpeedView() {
//       self.bringSubview(toFront: self.networkSpeedView)
       self.isShowOnce = false;
       self.networkSpeedView.isHidden = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
